<template>
  <div class="login-container">
    <div v-if="showLogo" class="logo">
      <img src="./assets/logo.png" />
    </div>
    <el-alert
      v-if="isIE"
      title="我们强烈建议您使用较新版本的 Chrome、 Firefox、 Safari 等浏览器,若使用IE浏览器可能会有部分功能缺失等情况!"
      type="warning"
      show-icon
      :closable="false"
    />
    <div class="systemTitle">
      <div style="font-size: 30px; margin-bottom: 8px; font-weight: 600">{{ title }}</div>
    </div>
    <div class="loginDiv">
      <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left">
        <!-- <div class="title-container">
          <h3 class="title">Login Form1</h3>
        </div>-->
        <p class="helloCSS">Hello!</p>
        <div class="textCSS">欢迎您登录{{ title }}</div>
        <el-form-item prop="loginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            v-model="loginForm.loginName"
            placeholder="用户名"
            name="loginName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <!-- <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span> -->
        </el-form-item>
        <el-button :loading="loading" type="primary" class="loginBtn" @click.native.prevent="handleLogin">
          登 录
        </el-button>

        <!-- <div class="tips">
        <span style="margin-right:20px;">loginName: admin123</span>
        <span> password: any</span>
        </div>-->
      </el-form>
    </div>
    <div class="versionText">v1.9.2</div>
  </div>
</template>

<script>
import defaultSettings from '@/settings'
// import { validUsername } from '@/utils/validate'
import md5 from 'js-md5'
export default {
  name: 'Login',
  data() {
    return {
      title: defaultSettings.title,
      showLogo: false,
      loginForm: {
        loginName: '',
        password: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isIE: false
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.isIE = this.checkIE()
  },
  methods: {
    checkIE() {
      if (!!window.ActiveXObject || 'ActiveXObject' in window) {
        return true
      } else {
        return false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const data = JSON.parse(JSON.stringify(this.loginForm))
          data.password = md5(data.password)
          this.$store
            .dispatch('user/login', data)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #e1eaf5;
$light_gray: #8f9bb3;
$cursor: rgb(18, 19, 19);

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 80%;
    .el-input__inner {
      font-size: 14px;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        background-color: transparent;
        background-image: none;
        /* //背景色透明  生效时长  过渡效果  启用时延迟的时间 */
        transition: background-color 50000s ease-in-out 0s;
        -webkit-text-fill-color: #000000 !important;
        box-shadow: 0 0 0px 1000px transparent inset !important;
        -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
      }
    }
  }

  .el-form-item {
    width: 340px;
    // border: 1px solid rgba(179, 166, 166, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border: 1px solid rgb(228, 224, 224);
    border-radius: 5px;
    margin-bottom: 30px;
    .el-form-item__content {
      line-height: 30px;
      background-color: #ffffff;
    }
  }
}
</style>

<style lang="less" scoped>
@bg: #2d3a4b;
@dark_gray: #889aa4;
@light_gray: #eee;
/deep/.el-alert {
  width: 750px;
  margin: 0 auto;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: @bg;
  background: url(./assets/background.png) no-repeat center;
  background-size: cover;
  overflow: hidden;
  .logo {
    position: absolute;
    top: 6vh;
    left: 8vh;
  }
  .systemTitle {
    position: absolute;
    color: #ffffff;
    top: 17%;
    left: 22%;
  }
  /deep/.el-form-item {
    border: 1px solid #ffffff;
  }
  .loginBtn {
    margin-bottom: 30px;
    width: 340px;
    height: 47px;
    font-size: 16px;
  }
  .loginDiv {
    height: 600px;
    width: 540px;
    position: relative;
    top: 18vh;
    left: 60%;
    background: url(./assets/loginDiv.png) no-repeat center;
    background-size: cover;
  }
  .login-form {
    position: relative;
    max-width: 100%;
    left: 100px;
    top: 72px;
    overflow: hidden;
  }
  .helloCSS {
    font-size: 30px;
    font-weight: 500;
    color: #ffffff;
  }
  .textCSS {
    color: #ffffff;
    width: 340px;
    font-weight: 500;
    font-size: 22px;
    padding-bottom: 50px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: @light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 12px;
    font-size: 16px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .versionText {
    color: #ffffff;
    position: absolute;
    bottom: 10px;
    right: 10px;
  }
}
</style>
